package com.ghy.web.backend;

import com.ghy.entity.EbProductCategory;
import com.ghy.service.ProductClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/productClass")
public class ProductClassController {
    @Autowired
    private ProductClassService productClassService;
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1")int page,
                       @RequestParam(value = "pageSize",defaultValue = "5")int pageSize){
        List<EbProductCategory> ebProductCategories = productClassService.listByPage();
        Integer totalPage = productClassService.getMaxPage(pageSize);
        List<EbProductCategory> parentCategory = new ArrayList<>();
        List<EbProductCategory> orderlyCategory = new ArrayList<>();
        List<EbProductCategory> outList = new ArrayList<>();
        for (int i = 0;i < ebProductCategories.size();++i){
            if(ebProductCategories.get(i).getEpcId()==ebProductCategories.get(i).getEpcParentId()){
                parentCategory.add(ebProductCategories.get(i));
                orderlyCategory.add(ebProductCategories.get(i));
                for (int m = 0;m < ebProductCategories.size();++m){
                    if (ebProductCategories.get(i).getEpcId()==ebProductCategories.get(m).getEpcParentId()
                            &&ebProductCategories.get(m).getEpcId()!=ebProductCategories.get(m).getEpcParentId()){
                        orderlyCategory.add(ebProductCategories.get(m));
                    }
                }
            }
        }
        int i = (page - 1) * pageSize;
        if (orderlyCategory.get(i).getEpcId() != orderlyCategory.get(i).getEpcParentId()){
            for(int m = 0;m < parentCategory.size();++m){
                if (parentCategory.get(m).getEpcId() == orderlyCategory.get(i).getEpcParentId()){
                    outList.add(parentCategory.get(m));
                    break;
                }
            }
            for (int m = i;m < orderlyCategory.size() && m < i + pageSize - 1;++m){
                outList.add(orderlyCategory.get(m));
            }
        }else {
            for (int m = i;m < orderlyCategory.size() && m < i + pageSize;++m){
                outList.add(orderlyCategory.get(m));
            }
        }
        //分页查询结果
        model.addAttribute("list",outList);
        //当前页
        model.addAttribute("currPage",page);
        //总页数
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("tag",3);
        return "manage/productClass";
    }

    @RequestMapping("/to_save")
    public String toSave(Model model){
        List<EbProductCategory> ebProductCategories = productClassService.listByPage();
        model.addAttribute("ebProductCategory",ebProductCategories);
        model.addAttribute("tag",3);
        return "manage/productClass-add";
    }
    @RequestMapping("/save")
    public String saveCommit(EbProductCategory ebProductCategory){
        System.out.println("ebProductClass==>"+ebProductCategory);
        int result = productClassService.save(ebProductCategory);
        if (result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete/{epcId}")
    public String delete(@PathVariable("epcId") Integer epcId){
        int result = productClassService.delete(epcId);
        if(result==0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/edit")
    public String edit(Model model,@RequestParam("epcId") int epcId){
        System.out.println("点击修改跳过来了，epcId="+epcId);
        EbProductCategory ebProductCategory = productClassService.select(epcId);
        List<EbProductCategory> ebProductCategories = productClassService.listByPage();
        model.addAttribute("ebProductCategories",ebProductCategories);
        model.addAttribute("ebProductCategory",ebProductCategory);
        model.addAttribute("tag",3);
        return "manage/productClass-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbProductCategory ebProductCategory){
        System.out.println("ebProductCategory==>"+ebProductCategory);
        Integer result = productClassService.update(ebProductCategory);
        if(result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
}
